<?php

error_reporting(E_ALL);
ini_set('include_path', get_include_path() .
    PATH_SEPARATOR . dirname(__FILE__) . '/../library');

define('SCRIPT_DIR', dirname(__FILE__));

require 'Zend/Console/Getopt.php';

try {
    $opts = new Zend_Console_Getopt(
        array(
            'project|p=w'  => 'Defines the project <project> and creates if nonexistent',
            'module|m=w'  => 'Creates the module <module>',
            'verbose|v'  => 'Shows detailed information',
            'help|h' => 'Shows the help message',
            )
        );
    $opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
    echo $e->getUsageMessage();
    exit;
}

class InitProject
{
    private $opts;

    public function __construct($opts)
    {
        $this->opts = $opts;
    }

    public function initModuleArray($name)
    {
        return array
            (
                'module' => $name,
                'controllers' => $name . '/controllers',
                'forms' => $name . '/forms',
                'models' => $name . '/models',
                'views' => $name . '/views',
                'scripts' => $name . '/views/scripts',
                'scripts_index' => $name . '/views/scripts/index',
            );
    }

    public function createDirsFromArray($dirs)
    {
        foreach ($dirs as $dir) {
            if (!mkdir($dir)) {
                echo "Failed to created $dir, aborting.\n";
                exit;
            }
            if (isset($this->opts->verbose)) {
                echo "$dir created.\n";
            }
        }
    }

    public function populateDirs($dirs)
    {
        $controller = $dirs['controllers'] . '/IndexController.php';
        $view = $dirs['scripts_index'] . '/index.phtml';
        $files = array
        (
            'controller' => array
            (
                SCRIPT_DIR . '/templates/controllers/IndexController.php',
                $controller,
            ),
            'view' => array
            (
                SCRIPT_DIR . '/templates/views/scripts/index.phtml',
                $view,
            )
        );

        foreach ($files as $k => $file) {
            if ($k == 'controller') {
                file_put_contents(
                    $file[1],
                    file_get_contents($file[0])
                );
                continue;
            }
            file_put_contents(
                $file[1],
                file_get_contents($file[0])
            );
        }
    }

    public function createModule()
    {
        if (isset($this->opts->verbose)) {
            echo "Beginning module <{$this->opts->module}> directory creation.\n";
        }

        $dirs = $this->initModuleArray($this->opts->module);
        $this->createDirsFromArray($dirs);
        $this->populateDirs($dirs);

        echo "Module <{$this->opts->module}> created.\n";
    }
}

/*
 Main
 */

if (isset($opts->help)) {
    echo $opts->getUsageMessage();
    exit;
}

// Module creation
if (isset($opts->module)) {
    if (is_dir($opts->module)) {
        echo "Module <{$opts->module}> already exists, aborting.\n";
        exit;
    }

    $project = new InitProject($opts);
    $project->createModule();
}
